// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue';
import iView from 'iview';
import VueRouter from 'vue-router';
import { sync } from 'vuex-router-sync';
import Vuex from 'vuex';
import 'iview/dist/styles/iview.css';
import Cookies from 'js-cookie';
import VueI18n from 'vue-i18n';
// import zhLocale from 'iview/src/locale/lang/zh-CN';
// import enLocale from 'iview/src/locale/lang/en-US';
import App from './App.vue';
import store from './store';

import router from './router';
import axios from './utils/fetch';
// import Locales from './locale';


Vue.use(VueRouter);
Vue.use(Vuex);
Vue.use(iView);
Vue.use(VueI18n);
// VUE配置项目
Vue.prototype.axios = axios;
sync(store, router);

// 路由配置
// const RouterConfig = {
//   // mode: 'history',
//   routes: router,
// };

// const router = new VueRouter(RouterConfig);


// // 自动设置语言
// const navLang = navigator.language;
// const localLang = (navLang === 'zh-CN' || navLang === 'en-US') ? navLang : false;
// const lang = window.localStorage.getItem('language') || localLang || 'zh-CN';

// Vue.config.lang = lang;

// // 多语言配置
// const locales = Locales;
// const mergeZH = Object.assign(zhLocale, locales['zh-CN']);
// const mergeEN = Object.assign(enLocale, locales['en-US']);
// Vue.locale('zh-CN', mergeZH);
// Vue.locale('en-US', mergeEN);

// 判断访问网站地址
router.beforeEach((to, from, next) => {
  console.log('Cookies');
  console.log(Cookies.get('password'));
  // Cookies.remove('user');
  // Cookies.remove('password');
  iView.LoadingBar.start();
  if (Cookies.get('locking') === '1' && to.name !== 'locking') {  // 判断当前是否是锁定状态
    iView.LoadingBar.finish();
    next(false);
    router.replace({
      name: 'locking',
    });
  } else if (Cookies.get('locking') === '0' && to.name === 'locking') {
    iView.LoadingBar.finish();
    next(false);
  } else if (!Cookies.get('user') && to.name !== 'login') {
    iView.LoadingBar.finish();  // 判断是否已经登录且前往的页面不是登录页
    next({
      name: 'login',
    });
  } else if (Cookies.get('user') && to.name === 'login') {
    iView.LoadingBar.finish(); // 判断是否已经登录且前往的是登录页
    next({
      name: 'home',
    });
  } else {
    iView.LoadingBar.finish();
    next();
  }
});

// 状态管理
/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  store,
  render: h => h(App),
});
